package com.sp.scmdb.jobImpl;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;

import com.sp.scmdb.entity.EntryRecord;
import com.sp.scmdb.entity.LabelIO;
import com.sp.scmdb.job.DealWithEntryRecordJob;
import com.sp.scmdb.service.ParseDataService;

public class DealWithEntryRecordJobImpl implements DealWithEntryRecordJob {

	private static Log log = LogFactory.getLog(DealWithEntryRecordJobImpl.class);
	
	private ParseDataService parseDataService;

	@Autowired
	public void setParseDataService(ParseDataService parseDataService) {
		this.parseDataService = parseDataService;
	}
	
	public void doParseEntryRecord() {
		parseEntryRecord();
	}
	
	private void parseEntryRecord() {
		try {
			String hql = "from EntryRecord";
			List<EntryRecord> entryRecordList = parseDataService.findEntryRecord(hql);
			if(CollectionUtils.isNotEmpty(entryRecordList)) {
				for(EntryRecord entryRecord : entryRecordList) {
					List<LabelIO> labelIoList = parseDataService.queryLabel("from LabelIO where labelID = " + entryRecord.getLabelID() 
							//+ " and outTime is null and inTime >= DATEADD(MINUTE, -5, '2013-01-07 10:29:52.000')");
					+ " and outTime is null and inTime >= DATEADD(MINUTE, -5, GETDATE())");
					if(CollectionUtils.isEmpty(labelIoList)) {
						if(!parseDataService.isOutCardRecord(entryRecord.getEmpID())) {
							entryRecord.setInTime(new DateTime());
							parseDataService.updateEntryRecord(entryRecord);
						}
					}
				}
			}
		} catch (Exception e) {
			log.error("operator parseOldOriginal error:" + e.getMessage());
		}
	}
}
